Test Setup Failed
Pull Request — master (#169)
by Jonathan
03:40
created

admin.js ➔ addFieldMappingRow   A

Complexity

Conditions 1
Paths 2

Size

Total Lines 15

Duplication

Lines 15
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
nc 2
nop 0
dl 15
loc 15
rs 9.4285
c 0
b 0
f 0
1 View Code Duplication
( function( $ ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
2
3
	function salesforceObjectFields() {
4
5
		var delay = ( function() {
6
			var timer = 0;
7
			return function( callback, ms ) {
8
				clearTimeout ( timer );
9
				timer = setTimeout( callback, ms );
10
			};
11
		}() );
12
13
		if ( 0 === $( '.salesforce_record_types_allowed > *' ).length ) {
14
			$( '.salesforce_record_types_allowed' ).hide();
15
		}
16
		if ( 0 === $( '.salesforce_record_type_default > *' ).length ) {
17
			$( '.salesforce_record_type_default' ).hide();
18
		}
19
		if ( 0 === $( '.pull_trigger_field > *' ).length ) {
20
			$( '.pull_trigger_field' ).hide();
21
		}
22
23
		$( '#salesforce_object' ).on( 'change', function( el ) {
0 ignored issues
show
Unused Code introduced by
The parameter el is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
24
			var that = this;
25
			var delayTime = 1000;
26
			delay( function() {
27
				var data = {
28
					'action': 'get_salesforce_object_description',
29
					'include': [ 'fields', 'recordTypeInfos' ],
30
					'field_type': 'datetime',
31
					'salesforce_object': that.value
32
				};
33
				$.post( ajaxurl, data, function( response ) {
0 ignored issues
show
Bug introduced by
The variable ajaxurl seems to be never declared. If this is a global, consider adding a /** global: ajaxurl */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
34
35
					var recordTypesAllowedMarkup = '',
36
recordTypeDefaultMarkup = '',
37
dateMarkup = '';
38
39
					if ( 0 < $( response.data.recordTypeInfos ).length ) {
40
						recordTypesAllowedMarkup += '<label for="salesforce_record_types_allowed">Allowed Record Types:</label><div class="checkboxes">';
41
						$.each( response.data.recordTypeInfos, function( index, value ) {
42
							recordTypesAllowedMarkup += '<label><input type="checkbox" class="form-checkbox" value="' + index + '" name="salesforce_record_types_allowed[' + index + ']" id="salesforce_record_types_allowed-' + index + '"> ' + value + '</label>';
43
						});
44
						recordTypesAllowedMarkup += '</div>';
45
46
47
						recordTypeDefaultMarkup += '<label for="salesforce_record_type_default">Default Record Type:</label>';
48
						recordTypeDefaultMarkup += '<select name="salesforce_record_type_default" id="salesforce_record_type_default"><option value="">- Select record type -</option>';
49
						$.each( response.data.recordTypeInfos, function( index, value ) {
50
							recordTypeDefaultMarkup += '<option value="' + index + '">' + value + '</option>';
51
						});
52
					}
53
54
					$( '.salesforce_record_types_allowed' ).html( recordTypesAllowedMarkup );
55
					$( '.salesforce_record_type_default' ).html( recordTypeDefaultMarkup );
56
57
					if ( 0 < $( response.data.fields ).length ) {
58
						dateMarkup += '<label for="pull_trigger_field">Date field to trigger pull:</label>';
59
						dateMarkup += '<select name="pull_trigger_field" id="pull_trigger_field"><option value="">- Select date field -</option>';
60
						$.each( response.data.fields, function( index, value ) {
61
							dateMarkup += '<option value="' + value.name + '">' + value.label + '</option>';
62
						});
63
						dateMarkup += '</select>';
64
						dateMarkup += '<p class="description">These are date fields that can cause WordPress to pull an update from Salesforce, according to the <code>salesforce_pull</code> class.</p>';
65
					}
66
67
					$( '.pull_trigger_field' ).html( dateMarkup );
68
69
					if ( '' !== recordTypesAllowedMarkup ) {
70
						$( '.salesforce_record_types_allowed' ).show();
71
					} else {
72
						$( '.salesforce_record_types_allowed' ).hide();
73
					}
74
					if ( '' !== recordTypeDefaultMarkup ) {
75
						$( '.salesforce_record_type_default' ).show();
76
					} else {
77
						$( '.salesforce_record_type_default' ).hide();
78
					}
79
80
					if ( '' !== dateMarkup ) {
81
						$( '.pull_trigger_field' ).show();
82
					} else {
83
						$( '.pull_trigger_field' ).hide();
84
					}
85
				});
86
			}, delayTime );
87
		});
88
	}
89
90
	function addFieldMappingRow() {
91
		$( '#add-field-mapping' ).click( function() {
92
			var salesforceObject = $( '#salesforce_object' ).val();
93
			var wordpressObject = $( '#wordpress_object' ).val();
94
			var rowKey = Math.floor( Date.now() / 1000 );
95
			$( this ).text( 'Add another field mapping' );
96
			if ( '' !== wordpressObject && '' !== salesforceObject ) {
97
				fieldmapFields( wordpressObject, salesforceObject, rowKey );
98
				$( this ).parent().find( '.missing-object' ).remove();
99
			} else {
100
				$( this ).parent().prepend( '<div class="error missing-object"><span>You have to pick a WordPress object and a Salesforce object to add field mapping.</span></div>' );
101
			}
102
			return false;
103
		});
104
	}
105
106
107
	function fieldmapFields( wordpressObject, salesforceObject, rowKey ) {
108
		var data = {
109
			'action': 'get_wp_sf_object_fields',
110
			'wordpress_object': wordpressObject,
111
			'salesforce_object': salesforceObject
112
		};
113
		$.post( ajaxurl, data, function( response ) {
0 ignored issues
show
Bug introduced by
The variable ajaxurl seems to be never declared. If this is a global, consider adding a /** global: ajaxurl */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
114
115
			var wordpress = '';
116
			var salesforce = '';
117
			var markup = '';
118
119
			wordpress += '<select name="wordpress_field[' + rowKey + ']" id="wordpress_field-' + rowKey + '">';
120
			wordpress += '<option value="">- Select WordPress field -</option>';
121
			$.each( response.data.wordpress, function( index, value ) {
122
				wordpress += '<option value="' + value.key + '">' + value.key + '</option>';
123
			});
124
			wordpress += '</select>';
125
126
			salesforce += '<select name="salesforce_field[' + rowKey + ']" id="salesforce_field-' + rowKey + '">';
127
			salesforce += '<option value="">- Select Salesforce field -</option>';
128
			$.each( response.data.salesforce, function( index, value ) {
129
				salesforce += '<option value="' + value.name + '">' + value.label + '</option>';
130
			});
131
			salesforce += '</select>';
132
133
			markup = '<tr><td class="column-wordpress_field">' + wordpress + '</td><td class="column-salesforce_field">' + salesforce + '</td><td class="column-is_prematch"><input type="checkbox" name="is_prematch[' + rowKey + ']" id="is_prematch-' + rowKey + '" value="1" /><td class="column-is_key"><input type="checkbox" name="is_key[' + rowKey + ']" id="is_key-' + rowKey + '" value="1" /></td><td class="column-direction"><div class="radios"><label><input type="radio" value="sf_wp" name="direction[' + rowKey + ']" id="direction-' + rowKey + '-sf-wp">  Salesforce to WordPress</label><label><input type="radio" value="wp_sf" name="direction[' + rowKey + ']" id="direction-' + rowKey + '-wp-sf">  WordPress to Salesforce</label><label><input type="radio" value="sync" name="direction[' + rowKey + ']" id="direction-' + rowKey + '-sync" checked>  Sync</label></div></td><td class="column-is_delete"><input type="checkbox" name="is_delete[' + rowKey + ']" id="is_delete-' + rowKey + '" value="1" /></td></tr>';
134
			$( 'table.fields tbody' ).append( markup );
135
136
		});
137
	}
138
139
	function pushAndPullObjects() {
140
		$( '.salesforce_user_ajax_message' ).hide();
141
		if ( 0 < $( '#wordpress_object_ajax' ).length ) {
142
			$( '.push_to_salesforce_button' ).on( 'click', function() {
143
				var wordpressObject = $( '#wordpress_object_ajax' ).val();
144
				var wordpressId = $( '#wordpress_id_ajax' ).val();
145
				var data = {
146
					'action': 'push_to_salesforce',
147
					'wordpress_object': wordpressObject,
148
					'wordpress_id': wordpressId
149
				};
150
				$.post( ajaxurl, data, function( response ) {
0 ignored issues
show
Bug introduced by
The variable ajaxurl seems to be never declared. If this is a global, consider adding a /** global: ajaxurl */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
151
					if ( true === response.success ) {
152
						updateSalesforceUserSummary();
153
						$( '.salesforce_user_ajax_message' ).width( $( '.mapped-salesforce-user' ).width() - 27 );
154
						$( '.salesforce_user_ajax_message' ).html( '<p>This object has been pushed to Salesforce.</p>' ).fadeIn().delay( 4000 ).fadeOut();
155
					}
156
				});
157
				return false;
158
			});
159
		}
160
		$( '.pull_from_salesforce_button' ).on( 'click', function() {
161
			var salesforceId = $( '#salesforce_id_ajax' ).val();
162
			var wordpressObject = $( '#wordpress_object_ajax' ).val();
163
			var data = {
164
				'action': 'pull_from_salesforce',
165
				'salesforce_id': salesforceId,
166
				'wordpress_object': wordpressObject
167
			};
168
			$.post( ajaxurl, data, function( response ) {
0 ignored issues
show
Bug introduced by
The variable ajaxurl seems to be never declared. If this is a global, consider adding a /** global: ajaxurl */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
169
				if ( true === response.success ) {
170
					updateSalesforceUserSummary();
171
					$( '.salesforce_user_ajax_message' ).width( $( '.mapped-salesforce-user' ).width() - 27 );
172
					$( '.salesforce_user_ajax_message' ).html( '<p>This object has been pulled from Salesforce.</p>' ).fadeIn().delay( 4000 ).fadeOut();
173
				}
174
			});
175
			return false;
176
		});
177
	}
178
179
	function updateSalesforceUserSummary() {
180
		var mappingId = $( '#mapping_id_ajax' ).val();
181
		var data = {
182
			'action': 'refresh_mapped_data',
183
			'mapping_id': mappingId
184
		};
185
		$.post( ajaxurl, data, function( response ) {
0 ignored issues
show
Bug introduced by
The variable ajaxurl seems to be never declared. If this is a global, consider adding a /** global: ajaxurl */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
186
			if ( true === response.success ) {
187
				$( 'td.last_sync_message' ).text( response.data.last_sync_message );
188
				$( 'td.last_sync_action' ).text( response.data.last_sync_action );
189
				$( 'td.last_sync_status' ).text( response.data.last_sync_status );
190
				$( 'td.last_sync' ).text( response.data.last_sync );
191
				if ( '1' === response.data.last_sync_status ) {
192
					$( 'td.last_sync_status' ).text( 'success' );
193
				}
194
			}
195
		});
196
	}
197
198
	function clearSfwpCacheLink() {
199
		$( '#clear-sfwp-cache' ).click( function() {
200
			var data = {
201
				'action': 'clear_sfwp_cache'
202
			};
203
			var that = $( this );
204
			$.post( ajaxurl, data, function( response ) {
0 ignored issues
show
Bug introduced by
The variable ajaxurl seems to be never declared. If this is a global, consider adding a /** global: ajaxurl */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
205
				if ( true === response.success && true === response.data.success ) {
206
					that.parent().html( response.data.message ).fadeIn();
207
				}
208
			});
209
			return false;
210
		});
211
	}
212
213
	// as the drupal plugin does, we only allow one field to be a prematch or key
214
	$( document ).on( 'click', '.column-is_prematch input', function() {
215
		$( '.column-is_prematch input' ).not( this ).prop( 'checked', false );
216
	});
217
218
	$( document ).on( 'click', '.column-is_key input', function() {
219
		$( '.column-is_key input' ).not( this ).prop( 'checked', false );
220
	});
221
222
	$( document ).ready( function() {
223
224
		var timeout;
225
		$( '#wordpress_object, #salesforce_object' ).on( 'change', function() {
226
			clearTimeout( timeout );
227
			timeout = setTimeout( function() {
228
				$( 'table.fields tbody tr' ).fadeOut();
229
				$( 'table.fields tbody tr' ).remove();
230
			}, 1000 );
231
		});
232
233
		// todo: need to fix this so it doesn't run all the spinners at the same time when there are multiples on the same page
234
		$( document ).ajaxStart( function() {
235
			$( '.spinner' ).addClass( 'is-active' );
236
		}).ajaxStop( function() {
237
			$( '.spinner' ).removeClass( 'is-active' );
238
		});
239
		salesforceObjectFields();
240
		addFieldMappingRow();
241
		pushAndPullObjects();
242
		clearSfwpCacheLink();
243
	});
244
245
}( jQuery ) );
246